import numpy as np
from matplotlib import pyplot as plt, axes, font_manager
import matplotlib
import scipy.optimize as opt
Zh_Font = matplotlib.font_manager.FontProperties(fname="../venv/include/SourceHanSansSC-Bold.otf")
plt.rc('font', family='Times New Roman')
plt.rc('font', size=16)
plt.rc('font', weight='bold')
legend_font = {
    'fname': 'C:/Windows/fonts/simhei.ttf',  # 引用系统字体
    'style': 'normal',
    'size': 10,  # 字号
    'weight': "normal",  # 是否加粗，不加粗
}
import random
# 迭代次数 200     成本区间
X = []
Y = []
X1 = []
Y1 = []
for i in range(10):
    X.append(random.randint(0, 10))
    Y.append(round(random.uniform(0.7, 0.9), 3))
for i in range(5):
    X.append(random.randint(10, 15))
    Y.append(round(random.uniform(0.6, 0.7), 3))
for i in range(5):
    X.append(random.randint(15, 20))
    Y.append(round(random.uniform(0.5, 0.6), 3))
for i in range(10):
    X.append(random.randint(20, 30))
    Y.append(round(random.uniform(0.4, 0.6), 3))
for i in range(170):
    X.append(random.randint(30, 200))
    Y.append(round(random.uniform(0.37, 0.4), 3))
X.sort(reverse=True)
Y.sort(reverse=False)
xMax = np.array(X).max()
xMin = np.array(X).min()
yMax = np.array(Y).max()
yMin = np.array(Y).min()
plt.xticks(np.arange(0, xMax+10, 20), fontsize=10, fontproperties=Zh_Font)
plt.yticks(np.arange(0, 1.0, 0.1), fontsize=10, fontproperties=Zh_Font)
plt.title("适应度变化曲线", fontsize=13, fontproperties=Zh_Font)
plt.ylabel('IGD', fontsize=10, fontproperties=Zh_Font)
plt.xlabel('总群迭代次数', fontsize=10, fontproperties=Zh_Font)

def parabola(X, a, b, c):
    xx = []
    for i in range(len(X)):
        xx.append(a * np.exp(-b * X[i]) + c)
    return xx

optimizedParameters, pcov = opt.curve_fit(parabola, X, Y)
print(optimizedParameters)
plt.scatter(X, Y, s=8, label="最小适应度")
# plt.plot(X, Y, label="最小适应度")
plt.plot(X, parabola(X, *optimizedParameters), label="拟合适应度",c="red")
plt.legend(fontsize=10, prop=legend_font)
plt.show()
